package com.lc.file;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.junit.Before;
import org.junit.Test;

/**
 * @author 逝兮诚
 * @date 2017年6月21日 下午4:57:47 fileChannel的复习类
 */
public class AsyncFileChannelTest {

	static RandomAccessFile file = null;
	static FileChannel fileChannel = null;

	@Before
	public void before() {
		try {
			file = new RandomAccessFile("src/main/resources/nio-data.txt", "rwd");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	static FileChannel fileChannel() {
		if (fileChannel == null) {
			try {
				file = new RandomAccessFile("src/main/resources/nio-data.txt", "rwd");
				fileChannel = file.getChannel();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		return fileChannel;
	}

	/**
	 * 读取文件
	 */
	@Test
	public void read() {
		FileChannel fileChannel = file.getChannel();
		ByteBuffer readBuf = ByteBuffer.allocate(64);
		try {
			int readsize = fileChannel.read(readBuf);
			while (readsize != -1) {
				readBuf.flip();
				System.out.print(Charset.forName("UTF-8").decode(readBuf));
				readBuf.clear();
				readsize = fileChannel.read(readBuf);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 写入文件
	 */
	@Test
	public void write() {
		FileChannel fileChannel = file.getChannel();
		ByteBuffer writeBuf = ByteBuffer.allocate(64);
		try {
			fileChannel.position(fileChannel.size());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			String writeData = sdf.format(new Date()) + " write\r\n";
			writeBuf.put(writeData.getBytes("UTF-8"));
			writeBuf.flip();
			while (writeBuf.hasRemaining()) {
				fileChannel.write(writeBuf);
			}
			writeBuf.clear();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 异步读写文件
	 */
	public static void main(String... args) {
		AsyncFileChannelTest test = new AsyncFileChannelTest();
		AsyncFileChannelTest.ReadThread readThread = test.new ReadThread();
		readThread.start();
		AsyncFileChannelTest.WriteThread writeThread = test.new WriteThread();
		writeThread.start();
	}

	class ReadThread extends Thread {
		public void run() {
			fileChannel = fileChannel();
			ByteBuffer readBuf = ByteBuffer.allocate(64);
			try {
				int readsize = fileChannel.read(readBuf);
				while (readsize != -1) {
					readBuf.flip();
					System.out.print(Charset.forName("UTF-8").decode(readBuf));
					readBuf.clear();
					readsize = fileChannel.read(readBuf);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	class WriteThread extends Thread {
		public void run() {
			fileChannel = fileChannel();
			ByteBuffer writeBuf = ByteBuffer.allocate(64);
			try {
				fileChannel.position(fileChannel.size());
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
				String writeData = sdf.format(new Date()) + " write\r\n";
				writeBuf.put(writeData.getBytes("UTF-8"));
				writeBuf.flip();
				while (writeBuf.hasRemaining()) {
					fileChannel.write(writeBuf);
				}
				writeBuf.clear();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
